<?php
namespace app\common\model;


class Prizes extends Base
{

    public static function search($zid)
    {
        $page = input('post.page/d');
        $pageSize = input('post.pageSize/d');
        $page = ($page < 1) ? 1 : $page;
        $pageSize = ($pageSize < 1 || $pageSize > 50) ? 10 : $pageSize;
        $self = new static();
        $query = $self->alias('a');
        $query->where('a.zid', '=', $zid);
        $query2 = clone $query;
        return [
            'total' => $query2->count('a.id'),
            'list' => $query->page($page, $pageSize)->order('a.addtime desc')->select()
        ];
    }
	
    public static function addShop($userInfo, $data)
    {
        $self = new static();
        if ($data['num'] == '') {
            $result = [
                'code' => 1,
                'message' => '中奖几率不能为空！'
            ];
        } else {
            switch ($data['type']) {
                case 1:
                    $add['name'] = '' . $data['vip'] . '天VIP';
                    $add['value'] = $data['vip'];
                    break;
                case 2:
                    $add['name'] = '' . $data['peie'] . '个配额';
                    $add['value'] = $data['peie'];
                    break;
                case 3:
                    $add['name'] = '' . $data['money'] . '元余额';
                    $add['value'] = $data['money'];
                    break;
                case 4:
                    $add['name'] = is_daili($data['dl']);
                    $add['value'] = $data['dl'];
                    break;
                case 5:
                    $add['name'] = '未中奖';
                    $add['value'] = '0';
                    break;
            }
            $add['zid'] = $userInfo['web_id'];
            $add['v'] = $data['num'];
            $add['type'] = $data['type'];
            $add['addtime'] = date('Y-m-d H:i:s');
            if ($self->insert($add)) {
                $result = [
                    'code' => 0,
                    'message' => '添加抽奖商品成功！'
                ];
            }
        }
        return $result;
    }

    public static function cj($userInfo)
    {
        $cj_need = config('sys_cj_need_jf');
        if ($userInfo['jf'] < $cj_need) {
            $result = [
                'code' => 1,
                'message' => '您的积分不足，攒够积分再来吧'
            ];
        } elseif (!self::getPrizes()) {
            $result = [
                'code' => 1,
                'message' => '站长还没上架抽奖商品，暂时无法抽奖哦'
            ];
        } else {
            $prizes = self::getPrizes();
            $i = 1;
            foreach ($prizes as $key => $val) {
                $arr[$i] = $val['v'];
                $i++;
            }
            $rid = getRand($arr); //根据概率获取奖项id
            $prize = $prizes[$rid - 1];
            if ($prize['type'] == 1) {
                Prizelog::add($userInfo, '抽中了：' . $prize['value'] . '天VIP');
                //开通时间
                $vip_start = date("Y-m-d");
                //计算开通的vip时长
                if ($userInfo['viptime']) {
                    $vip_time = date("Y-m-d", strtotime("+" . $prize['value'] . " day", strtotime($userInfo['viptime'])));
                } else {
                    $vip_time = date("Y-m-d", strtotime("+" . $prize['value'] . " day"));
                }
                $update = [
                    'vipstart' => $vip_start,
                    'viptime' => $vip_time,
                    'jf' => $userInfo['jf'] - $cj_need
                ];
                Users::updateByUid($userInfo['uid'], $update);
                $result = [
                    'code' => 0,
                    'message' => '恭喜您抽中了' . $prize['value'] . '天VIP'
                ];
            } elseif ($prize['type'] == 2) {
                Prizelog::add($userInfo, '抽中了：' . $prize['value'] . '个挂机配额');
                $update = [
                    'peie' => $userInfo['peie'] + $prize['value'],
                    'jf' => $userInfo['jf'] - $cj_need
                ];
                Users::updateByUid($userInfo['uid'], $update);
                $result = [
                    'code' => 0,
                    'message' => '恭喜您抽中了' . $prize['value'] . '个挂机配额'
                ];
            } elseif ($prize['type'] == 3) {
                Prizelog::add($userInfo, '抽中了：' . $prize['value'] . '元余额');
                $update = [
                    'money' => $userInfo['money'] + $prize['value'],
                    'jf' => $userInfo['jf'] - $cj_need
                ];
                Users::updateByUid($userInfo['uid'], $update);
                $result = [
                    'code' => 0,
                    'message' => '恭喜您抽中了' . $prize['value'] . '元账户余额'
                ];
            } elseif ($prize['type'] == 4) {
                Prizelog::add($userInfo, '抽中了：' . is_daili($prize['value']) . '');
                //代理等级
                $dj = $prize['value'];
                //判断当前是否是代理,而且抽中的代理等级大于当前等级才执行
                if ($userInfo['dlpower'] == 0 || $dj > $userInfo['dlpower']) {
                    //开通时间
                    $vip_start = date("Y-m-d");
                    //计算开通的vip时长
                    if ($userInfo['viptime']) {
                        $vip_time = date("Y-m-d", strtotime("+" . config('sys_dl_give_vip_' . $prize['value'] . '') . " day", strtotime($userInfo['viptime'])));
                    } else {
                        $vip_time = date("Y-m-d", strtotime("+" . config('sys_dl_give_vip_' . $prize['value'] . '') . " day"));
                    }
                    Users::updateByUid($userInfo['uid'], [
                        'dlpower' => $dj,
                        'vipstart' => $vip_start,
                        'viptime' => $vip_time,
                        'money' => $userInfo['money'] + config('sys_dl_give_rmb_' . $dj . ''),
                        'dlzk' => config('sys_dl_give_z_' . $dj . ''),
                        'jf' => $userInfo['jf'] - $cj_need
                    ]);
                    $result = [
                        'code' => 0,
                        'message' => '恭喜您运气爆棚抽中了' . is_daili($prize['value']) . ''
                    ];
                } else {
                    $result = [
                        'code' => 0,
                        'message' => '恭喜您运气爆棚抽中了' . is_daili($prize['value']) . '，兑换失败，您当前是' . is_daili($userInfo['dlpower']) . ''
                    ];
                }
            } else {
                Users::updateByUid($userInfo['uid'], [
                    'jf' => $userInfo['jf'] - $cj_need
                ]);
                $result = [
                    'code' => 1,
                    'message' => '很遗憾，您没有中奖'
                ];
            }
        }
        return $result;
    }

    public static function delShopById($id)
    {
        $self = new static();
        return $self
            ->where('id', '=', $id)
            ->delete();
    }

    public static function isTodayCj($uid)
    {
        $self = new static();
        $isCj = $self->where(['uid' => $uid])->where('TO_DAYS(date)=TO_DAYS(NOW())')->find();
        if ($isCj) {
            return true;
        } else {
            return false;
        }
    }

    public static function updateById($id, $data)
    {
        $self = new static();
        $result = $self
            ->where('id', '=', $id)
            ->update($data);
        if ($result) {
            return $result;
        } else {
            return false;
        }
    }

    public static function getPrizes()
    {
        $self = new static();
        $result = $self
            ->where('zid', '=', WEB_ID)
            ->select();
        return $result;
    }
}